<?php


namespace app\admin\controller;

use databackup\src\Backup;
use \think\Db;
use \think\Cookie;
use \think\Session;
use app\admin\model\Admin as adminModel;//管理员模型
use app\admin\model\AdminMenu;
use app\admin\controller\Permissions;

class Order extends Permissions
{

    /**
     * 删除订单
     */
    public function order_delete()
    {
        $id = input('id');
        $file = input('file');
        $admin = \db('fit_order')->where('order_id', $id)->update(['is_delete'=>1]); //软删
        if ($admin >= 0) {
            $this->success('删除成功', 'order/'.$file);
        } else {
            $this->error('删除失败');
        }
    }

    

    /**
     * 待支付的订单管理
     */
    public function order_tobepaid()
    {
        $where['order_state'] = 1; //1为待支付标识
        $where['a.is_delete'] = 0;
        if (!empty($_POST['keywords'])) {
            $where['order_number'] = $_POST['keywords']; //订单号查询
        }

        $info = \db('fit_order')
            ->alias('a')
            ->join('fit_user b', 'a.fit_pid=b.fit_id')
            ->where($where)
            ->order('order_xdtime desc')
            ->paginate(10);

        $page = $info->render();
        $this->assign('page', $page);
        $this->assign('info', $info);
        return $this->fetch();
    }


    /**
     * 待收货的订单管理
     */
    public function order_pending_receipt()
    {
        $where['order_state'] = 2; //2为待收货标识
        $where['a.is_delete'] = 0;
        if (!empty($_POST['keywords'])) {
            $where['order_number'] = $_POST['keywords']; //订单号查询
        }

        $info = \db('fit_order')
            ->alias('a')
            ->join('fit_user b', 'a.fit_pid=b.fit_id')
            ->where($where)
            ->order('order_xdtime desc')
            ->paginate(10);
            
        $page = $info->render();
        $this->assign('page', $page);
        $this->assign('info', $info);
        return $this->fetch();
    }


    /**
     * 已完成订单管理
     */
    public function order_completed()
    {
        $where['order_state'] = 3; //3为已完成标识
        $where['a.is_delete'] = 0;
        if (!empty($_POST['keywords'])) {
            $where['order_number'] = $_POST['keywords']; //订单号查询
        }

        $info = \db('fit_order')
            ->alias('a')
            ->join('fit_user b', 'a.fit_pid=b.fit_id')
            ->where($where)
            ->order('order_xdtime desc')
            ->paginate(10);
            
        $page = $info->render();
        $this->assign('page', $page);
        $this->assign('info', $info);
        return $this->fetch();
    }


    /**
     * 已取消的订单管理
     */
    public function order_cancelled()
    {
        $where['order_state'] = 4; //4为已取消标识
        $where['a.is_delete'] = 0;
        if (!empty($_POST['keywords'])) {
            $where['order_number'] = $_POST['keywords']; //订单号查询
        }

        $info = \db('fit_order')
            ->alias('a')
            ->join('fit_user b', 'a.fit_pid=b.fit_id')
            ->where($where)
            ->order('order_xdtime desc')
            ->paginate(10);
            
        $page = $info->render();
        $this->assign('page', $page);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 同意退款
     */
    public function order_agree()
    {
        $id = input('id');
        $order = \db('fit_order')->where('order_id', $id)->find(); //订单信息
        $user = \db('fit_user')->where('fit_id', $order['fit_pid'])->find(); //用户信息
        $a = sendMoney($order['order_pay_money'], $user['fit_opid'], $user['fit_name']); //调起退款接口 成功时返回“SUCCESS"
        if ($a==1) {
            $b = \db('fit_order')->where('order_id', $id)->update(['order_state' => 4]);
            if ($b == 0) { //if ($order > 0) {     update()方法成功时返回1，失败返回0
                $fit_jifen = $user['fit_jifen']+$order['order_jifen'];
                \db('fit_user')->where('fit_id', $order['fit_pid'])->update(['fit_jifen'=>$fit_jifen]); //积分恢复
                $this->success('同意成功,已退款', 'order/order_cancelled');
            } else {
                $this->error('数据出错', 'order/order_pending_receipt');
            }
        }else{
            $this->error($a, 'order/order_pending_receipt');
        }

    }

}
